import { userService } from '@/services/user'
import globalService from '@/services/globalService'
import axios from 'axios'
const initState = () => {
  return {
    token: '',
    userInfo: '',
    account: {},
    menus: localStorage.getItem('menus') ? JSON.parse(localStorage.getItem('menus')) : [],
    permissionSet: localStorage.getItem('permissionSet') ? JSON.parse(localStorage.getItem('permissionSet')) : []
  }
}
const state = initState()
const getters = {
	permissionSet: state => {
		return state.permissionSet
	},
  menus: state => {
    return resetPrimaryMenus(state.menus || [])
  }
}
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    // setAxiosHeaders(token)
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_ACCOUNT: (state, account) => {
    state.account = Object.assign({}, state.account, account)
  },
  SET_MENUS: (state, menus) => {
    state.menus = menus
    localStorage.setItem('menus', JSON.stringify(menus))
  },
  SET_PERMISSION_SET: (state, permissionSet) => {
    state.permissionSet = permissionSet
    localStorage.setItem('permissionSet', JSON.stringify(permissionSet))
  },
  resetState(state) {
    state = initState()
  }
}


const actions = {
  getAccountInfo({ commit }) {
    // 好像不要
    userService
      .getLoginUserInfo({})
      .then((res) => {
        commit('SET_ACCOUNT', res.data)
      })
      .catch((e) => {
        console.log(e)
      })
  },
  getMenuList({ commit }) {
    globalService
      .getUserInfoResp({ systemFlag: 1 })
      .then(({ data }) => {
        commit('SET_MENUS', data.menus)
        commit('SET_PERMISSION_SET', data.permissionSet)
        commit('SET_USERINFO', {
          userName: data.userName,
          userRealName: data.userRealName,
          userCode: data.userCode,
          userMobile: data.userMobile
        })
        if (data && data.tenantCode) {
          localStorage.setItem('tenantCode', data.tenantCode)
        }
        if (data && data.tenantId) {
          localStorage.setItem('tenantId', data.tenantId)
        }
        if (data && data.tenantName) {
          localStorage.setItem('tenantName', data.tenantName)
        }
        if (data && data.userName) {
          localStorage.setItem('userName', data.userName)
        }
        if (data && data.userRealName) {
          localStorage.setItem('userRealName', data.userRealName)
        }
        if (data && data.userCode) {
          localStorage.setItem('userCode', data.userCode)
        }
        if (data && data.userMobile) {
          localStorage.setItem('userMobile', data.userMobile)
        }
      })
      .catch((e) => {})
  },
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', '')
      // localStorage.clear()
      resolve()
    })
  }
}

function setAxiosHeaders(token) {
  axios.defaults.headers.common['aioSsoTokenId'] = token
  axios.defaults.headers.common['ucSsoTokenId'] = token
}

function resetPrimaryMenus(arr) {
	let res = []
	if (arr.length === 0) {
		return false
	}
	function find(list) {
		list.forEach(it => {
			if (it.menuType === 3) {
				res.push(it.menuCode);
			} else if (it.menuType === 2 && it.children && it.children.length) {
				find(it.children)
			}
		})
	}
	find(arr)
	return res
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
